import configparser
import math
import time
from sqlalchemy import Column, String, create_engine, Integer
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
import requests

headers = {
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36",
    "cookie": ""
}

# 创建对象的基类:
Base = declarative_base()


class up_fans(Base):
    # 表的名字:
    __tablename__ = 'up_fans'

    # 表的结构:

    mid = Column(Integer, primary_key=True)
    down_time = Column(Integer)
    fans = Column(Integer)


def get_jl(cookie):
    url="https://api.bilibili.com/studio/growup/web/up/income/stat?type=0&s_locale=zh_CN"
    url2="https://api.bilibili.com/studio/growup/web/up/archive/income?all=1&type=0&page=1&size=10&s_locale=zh_CN"
    # url = "http://api.bilibili.com/x/web-interface/card?mid={}"
    headers['cookie'] = cookie
    res = requests.get(url2, headers=headers)
    res = res.json()
    return res
    # if res.get("code") == 0:
    #     # print(res.get("data").get("card").get("fans"))
    #     engine = create_engine("mysql+pymysql://root:zxczxc@192.168.2.8:3307/bilibili?charset=utf8")
    #     DBSession = sessionmaker(bind=engine)
    #     session = DBSession()
    #     new_up_fans = up_fans(mid=mid,
    #                           down_time=time.time(),
    #                           fans=res.get("data").get("card").get("fans")
    #                           )
    #     session.add(new_up_fans)
    #     session.commit()
    #     session.close()


def config():
    ck = []
    cp = configparser.RawConfigParser()
    cp.read('./config.ini')
    ck.append(cp.get('bilibili', 'cookie1'))
    ck.append(cp.get('bilibili', 'cookie2'))
    ck.append(cp.get('bilibili', 'cookie3'))
    # up_list = cp.get('up_list', 'up_list')

    return ck


if __name__ == '__main__':
    cookie = config()[0]
    print(get_jl(cookie))


